<?php

namespace apiend\models\searchs;

use common\db\SearchModelInterface;
use common\models\shop\ShopProduct as SearchModel;
use apiend\modules\shop\models\ShopStoreMinLogic;

/**
 * 商品检索模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class ProductSearch extends SearchModel implements SearchModelInterface {

    /**
     * 按数量条件检索
     * @param mixed $limit 查询数量
     * @return array
     */
    public function getStore() {
        return $this->hasOne(ShopStoreMinLogic::className(), ['id' => 'store_id']);
    }

    /**
     * 按数量条件检索
     * @param mixed $limit 查询数量
     * @return array
     */
    public function limitsSearch($limit = 6, $cacheDuration = 60) {
        $query = $this->buildQuery();
        if (!YII_DEBUG && $cacheDuration !== false) {
            $query->cache($cacheDuration);
        }
        if ($limit !== false) {
            $query->limit($limit);
        }
        return $query->all();
    }

    public function fields() {
        return [
            'id',
            'title' => 'name',
            'description',
            'cover' => 'thumb',
            'thumb',
            'price',
            'discount',
            'skus',
            'specCount',
            'link',
            'stats',
            'store',
            'created_at',
            'updated_at',
            'balanceRates',
            'status',
        ];
    }

}
